<?php
require_once( __DIR__ . '/inc/common.php' );

// Get the video id from URL.
// Such as "C0DPdy98e4c"
$search_query = @$_GET['q'];
$page = ( isset( $_GET['page'] ) && is_numeric( $_GET['page'] ) ) ? intval( $_GET['page'] ) : 1;

$templates = new League\Plates\Engine( 'themes/' . $config['theme'] );

if ( ! empty( $search_query ) ) {
	$search = new \Vioscope\Search();
	echo $templates->render( 'search', array(
		'entries' => $search->run_search( $search_query, $page ),
		// TODO: implement next/prev page
		'config' => $config,
		'info' => array(
			'title' => 'Search for ' . $search_query,
			'search_query' => $search_query,
			'page' => $page,
			'next_page_url' => $search->get_change_page_url(true),
			'previous_page_url' => $search->get_change_page_url(false),
		),
	) );
} else {
	echo $templates->render( 'search', array(
		'error' => 'No valid channel id provided'
	) );
}
